package com.zz.stream;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Test2 {
    public static void main(String args[]){
        //1.集合获取Stream流
        List<String> names = new ArrayList <>();
        Collections.addAll(names, "刘德华", "朱子晴", "朱德", "郭富城");
        List<String> list = names.stream().filter(s -> s.contains("德")).collect(Collectors.toList());
        System.out.println(list);

        //2.Set集合获取Stream流
        Set<String> set = new HashSet <>();
        Collections.addAll(set, "刘德华", "朱子晴", "朱德", "郭富城");
        Stream<String> stream = set.stream();
        stream.filter(s -> s.contains("朱")).forEach(s -> System.out.println(s));

        //使用Map集合获取Stream流
        Map<String, Double> map = new HashMap <>();
        map.put("白骨精", 145.9);
        map.put("牛魔王", 180.3);
        map.put("孙悟空", 156.4);
        /**
         * Map集合不能直接获取Stream流
         * 解决方法
         * 1.通过Set<String> keys = map.keySet();获取键流
         *  Collection<Double> values = map.values();获取值流
         *  2.使用Entry封装键值对：Set<Map.Entry<String, Double>> entries = map.entrySet();
         */
        Set<String> keys = map.keySet();
        Stream<String> ks = keys.stream();//获取到键的流

        Collection<Double> values = map.values();
        Stream<Double> vs = values.stream();

        Set<Map.Entry<String, Double>> entries = map.entrySet();
        Stream<Map.Entry<String, Double>> kvs = entries.stream();
        kvs.filter(e -> e.getKey().contains("牛")).forEach(s -> System.out.println(s.getKey() + "=" + s.getValue()));

        //数组获取Stream流
        String[] names2 = {"张护", "妞儿", "bab"};
        Stream<String> s1 = Arrays.stream(names2);
        Stream<String> s2 = Stream.of(names2);
    }
}
